/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.annotation.util;

import java.lang.reflect.Method;
import org.balisunrise.annotation.MenuAction;

/**
 *
 * @author Glauco
 */
public class MenuUtilities {

    private static final String MESSAGE = "Erro ao ler Annotation \"MenuItem\".";

    public Method method;
    public String module;
    public String[] folders;
    public String name;
    public String text;
    public String toolTipText;
    public String icon;
    

    public static MenuUtilities readMenuItem(Method methodMenuAction)
            throws AnnotationsUtilitiesException {

        if(methodMenuAction == null)
            throw new AnnotationsUtilitiesException(MESSAGE +
                    "\nParâmentro \"methodMenuAction\" não pode ser nulo!");

        if(!methodMenuAction.isAnnotationPresent(MenuAction.class))
            throw new AnnotationsUtilitiesException(MESSAGE +
                    "\nAnnotation \"MenuAction\" não encontrada!");

        if(methodMenuAction.getParameterTypes().length > 0){
            throw new AnnotationsUtilitiesException(MESSAGE +
                    "\nO método do item de menu não deve possuir parâmetros!");
        }
        
        MenuUtilities mu = new MenuUtilities();

        MenuAction action = (MenuAction) methodMenuAction.getAnnotation(
                MenuAction.class);
                
        mu.method = methodMenuAction;
        mu.module = action.module();
        mu.name = action.name();
        mu.text = action.text();
        mu.toolTipText = action.toolTipText();
        mu.icon = action.icon();

        String folder = action.folder();
        if(folder.length() == 0 || !folder.contains("/"))
            mu.folders = new String[]{folder};
        else{
            mu.folders = folder.split("/");
        }

        return mu;
    }
}
